/**********************************************************************
* Filename: Equipa.java
* Created: 2012/05/04
* @author Fernando Brito e Abreu
**********************************************************************/
package org.quasar.copaPaises.businessLayer;

import java.util.Set;
import java.util.HashSet;

public class Equipa
{
	public static Set<Equipa> allInstances = new HashSet<Equipa>();
	
	private Jogo jogo;
	private Pais seleccao;
	
	/**********************************************************************
	* Default constructor
	**********************************************************************/
	public Equipa()
	{
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Parameterized constructor
	* @param jogo the jogo to initialize
	* @param seleccao the seleccao to initialize
	**********************************************************************/
	public Equipa(Jogo jogo, Pais seleccao)
	{
		this.jogo = jogo;
		this.seleccao = seleccao;
		allInstances.add(this);
	}
	
	/**********************************************************************
	* ASSOCIATIVE2MEMBER getter for Equipa[*] <-> Jogo[1]
	* @return the jogo of the equipa
	**********************************************************************/
	public Jogo jogo()
	{
		return jogo;
	}
	
	/**********************************************************************
	* ASSOCIATIVE2MEMBER setter for Equipa[*] <-> Jogo[1]
	* @param jogo the jogo to set
	**********************************************************************/
	public void setJogo(Jogo jogo)
	{
		this.jogo = jogo;
	}
	
	/**********************************************************************
	* ASSOCIATIVE2MEMBER getter for Equipa[*] <-> Pais[1]
	* @return the seleccao of the equipa
	**********************************************************************/
	public Pais seleccao()
	{
		return seleccao;
	}
	
	/**********************************************************************
	* ASSOCIATIVE2MEMBER setter for Equipa[*] <-> Pais[1]
	* @param seleccao the seleccao to set
	**********************************************************************/
	public void setSeleccao(Pais seleccao)
	{
		this.seleccao = seleccao;
	}
	
	/**********************************************************************
	* MEMBER2ASSOCIATIVE getter for Equipa[*] <-> Membro[11..*]
	* @return the membro of the equipa
	**********************************************************************/
	public Set<Membro> membro()
	{
		Set<Membro> result = new HashSet<Membro>();
		for (Membro x : Membro.allInstances)
			if (x.equipa()  ==  this)
				result.add(x);
		return result;
	}
	
	/**********************************************************************
	* MEMBER2ASSOCIATIVE setter for Equipa[*] <-> Membro[11..*]
	* @param membro the membro to set
	**********************************************************************/
	public void setMembro(Set<Membro> membro)
	{
		for (Membro x : membro)
			x.setEquipa(this);
	}
	
	/**********************************************************************
	* MEMBER2MEMBER getter for Equipa[*] <-> Jogador[11..*]
	* @return the jogador of the equipa
	**********************************************************************/
	public Set<Jogador> jogador()
	{
		Set<Jogador> result = new HashSet<Jogador>();
		for (Membro x : Membro.allInstances)
			if (x.equipa()  ==  this)
				result.add(x.jogador());
		return result;
	}
	
	/**********************************************************************
	* MEMBER2MEMBER setter for Equipa[*] <-> Jogador[11..*]
	* @param jogador the jogador to set
	**********************************************************************/
	public void setJogador(Set<Jogador> jogador)
	{
		for (Jogador t : jogador){
			for (Membro x: Membro.allInstances){
				if (x.equipa() == this){
					x.setJogador(t);
//					System.out.println("IF");
				}
//				System.out.println(x);
			}
//		System.out.println(t);
		}
	}
	
	/* (non-Javadoc)
	* @see java.lang.Object#toString()
	*/
	@Override
	/**********************************************************************
	* Object serializer
	**********************************************************************/
	public String toString()
	{
		return "Equipa [jogo=" + jogo + ", seleccao=" + seleccao + "]\n";
	}
	
	//	-------------------------------------------------------------------------------
	//	INVARIANTS (TODO)
	/*
	inv JogadoresEquipaPertencemSelecao
		self.jogador->forAll(j : Jogador | (j.nacionalidade = self.seleccao))
	
	*/
}
